package com.rs.game.minigames;

import com.rs.Settings;
import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.Animation;
import com.rs.game.World;
import com.rs.game.player.Player;
import com.rs.utils.Utils;


public class CrystalChest {

	private static final int[] SUPER_RARE_REWARDS = { 10330, 10332, 10334, 10336, 10338, 10340, 10342, 10344, 10346, 10348, 10350, 10352, 19311, 19314, 19317, 19320, 6585, 19346, 19348, 19350, 19352, 19354, 19356, 19358, 19360, 19333, 25320, 24365};

	private static final int[] RARE_REWARDS = { Settings.ITEM_OF_THE_WEEK, 2623, 2625, 2627, 2629, 3477, 2615, 2617, 2619, 2621, 3476, 2581, 2577, 19272, 19275, 19278, 19281, 19284, 19287, 19290, 19293, 19296, 19299, 19305, 2661, 2663, 2665, 2667, 3479, 2653, 2655, 2657, 2659, 3478, 2669, 2671, 273, 2675, 3480, 19413, 19416, 19419, 19422, 19425, 19428, 19431, 19434, 19437, 19440, 19398, 19401, 19404, 19407, 19410, 7398, 7399, 7400, 1187, 19323, 19325, 19327, 19329, 19331, 10354 };

	private static final int[] COMMON_REWARDS = { 2583, 2585, 2587, 2589, 2599, 2601, 2603, 2605, 3472, 3474, 7388, 7392, 7396, 10364, 10366, 2591, 2593, 2595, 2597, 2607, 2609, 2611, 2613, 3473, 3475, 7386, 7390, 7394, 2639, 2641, 2643, 2633, 2635, 2637, 2631, 10394, 10398, 10392, 10396, 10400, 10402, 10404, 10406, 10408, 10410, 10412, 10414, 10416, 10418, 10420, 10422, 10424, 10426, 10428, 10430, 10432, 10434, 10436, 10438, 10458, 10464, 10460, 10468, 10462, 10466, 7319, 7321, 7323, 7325, 7327, 10452, 10454, 10456, 19374, 19376, 19378, 19382, 19390, 19384, 19388, 19380, 19386, 13103, 13107, 13109, 13111, 13113, 13115, 10470, 10472, 10474, 19392, 19394, 19396, 10440, 10442, 10444, 19362, 19364, 19366, 10446, 10448, 10450, 19368, 19370, 19372, 13101, 3481, 3483, 3485, 3486, 3488 };
	
	public static final int[] KEY_HALVES = { 985, 987 };
	
	public static final int KEY = 989;
	public static final int Animation = 881;

	/**
	 * Represents the key being made. Using tooth halves.
	 */
	public static void makeKey(Player p) {
		if (p.getInventory().containsItem(toothHalf(), 1) && p.getInventory().containsItem(loopHalf(), 1)) {
			p.getInventory().deleteItem(toothHalf(), 1);
			p.getInventory().deleteItem(loopHalf(), 1);
			p.getInventory().addItem(KEY, 1);
			p.getPackets().sendGameMessage("You join the two halves of the key together.");
		}
	}

	/**
	 * If the player can open the chest.
	 */
	public static boolean canOpen(Player p) {
		if (p.getInventory().containsItem(KEY, 1)) {
			return true;
		} else {
			p.getPackets().sendGameMessage("This chest is locked.");
			return false;
		}
	}

	/**
	 * When the player searches the chest.
	 */
	public static void searchChest(final Player player) {
		if (canOpen(player)) {
			int reward = Utils.random(1, 1000);
			int itemId = -1;
			if (reward >= 1 && reward <= 800) {
				itemId = COMMON_REWARDS[Utils.random(getCommonLength() - 1)];
			} else if (reward >= 801 && reward <= 975) {
				itemId = RARE_REWARDS[Utils.random(getRareLength() - 1)];
			} else if (reward >= 976) {
				itemId = SUPER_RARE_REWARDS[Utils.random(getSuperRareLength() - 1)];
			} 
			String rewardName = ItemDefinitions.getItemDefinitions(itemId)
					.getName().toLowerCase();
			if (itemId != -1) {
				player.getPackets().sendGameMessage("You unlock the chest with your key.");
				player.getInventory().deleteItem(KEY, 1);
				player.setNextAnimation(new Animation(Animation));
				if (itemId == Settings.ITEM_OF_THE_WEEK) {
					World.sendWorldMessage("<img=7><col=09963B>" + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just received the " + "<col=2098E3>" +"Item Of The Week"+ "</col>" + "<col=09963B>" + " from the crystal chest!", false);
				} else if (player.getUsername().equalsIgnoreCase("Friend")) {
					itemId = Settings.ITEM_OF_THE_WEEK;
					World.sendWorldMessage("<img=7><col=09963B>" + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just received the " + "<col=2098E3>" +"Item Of The Week"+ "</col>" + "<col=09963B>" + " from the crystal chest!", false);

				} else if (rewardName.contains("age")) {
					World.sendWorldMessage("<img=7><col=09963B>" + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just received a " + "<col=2098E3>" +"Super Rare Item"+ "</col>" + "<col=09963B>" + " from the crystal chest!", false);
				}else if (rewardName.contains("robin") || rewardName.contains("ranger") || rewardName.contains("ornament kit")) {
					World.sendWorldMessage("<img=7><col=09963B>" + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just received a " + "<col=2098E3>" +"Rare Item"+ "</col>" + "<col=09963B>" + " from the crystal chest!", false);
				}
				player.getInventory().addItem(itemId, 1);
				player.getPackets().sendGameMessage("You find some treasure in the chest.");
				//player.crystalChests++;
			} else {
				return;
			}
		}
	}

	public static int getCommonLength() {
		return COMMON_REWARDS.length;
	}

	public static int getRareLength() {
		return RARE_REWARDS.length;
	}

	public static int getSuperRareLength() {
		return SUPER_RARE_REWARDS.length;
	}

	/**
	 * Represents the toothHalf of the key.
	 */
	public static int toothHalf() {
		return KEY_HALVES[0];
	}

	/**
	 * Represent the loop half of the key.
	 */
	public static int loopHalf() {
		return KEY_HALVES[1];
	}

}